
import { ajax } from 'nc-lightapp-front';
import { AREA, URL, FIELDS } from '../../constance';
import {
        showSuccessInfo,
        showWarningInfo,
        showDeleteDialog,
        showBatchOperateInfo
} from '../../tool/messageUtil';
import { deleteCacheDataForList } from '../../tool/cacheDataManager';
import { buttonControl } from '../viewControl/buttonControl';
import { getLangByResId } from '../../tool/multiLangUtil';

export default function deleteBtnClick(props, record, index) {
        if (record && index >= 0) {
                // 行操作删除
                deleteOperation.call(this, props, record, index);
        } else {
                // 按钮删除
                showDeleteDialog({
                        beSureBtnClick: () => {
                                deleteOperation.call(this, props);
                        }
                });
        }
}

function deleteOperation(props, record, index) {
        let reqData = { infos: [] };

        let pks = [];
        let indexMap = new Map();
        if (record && index >= 0) {
                let pk = record[FIELDS.pk_head_fields].value;
                // 行删除
                reqData.infos.push({
                        id: pk,
                        ts: record[FIELDS.ts].value
                });
                indexMap.set(pk, index);
        } else {
                // 按钮删除
                let checkArr = props.table.getCheckedRows(AREA.listTableId);
                if (!checkArr || checkArr.length < 1) {
                        showWarningInfo(null, getLangByResId(this, '4001PUBMESSAGE-0000011')); /* 国际化处理： 请选择数据*/
                        return;
                }
                checkArr.map((row) => {
                        let pk = row.data.values[FIELDS.pk_head_fields].value;
                        reqData.infos.push({
                                id: pk,
                                ts: row.data.values[FIELDS.ts].value
                        });
                        indexMap.set(pk, row.index);
                });
        }

        ajax({
                url: URL.delete,
                data: reqData,
                success: (res) => {
                        if (res.success && res.data) {
                                // 提示信息
                                if (res.data.errMsg && res.data.errMsg.length > 0) {
                                        showBatchOperateInfo(null, res.data.message, res.data.errMsg);
                                } else {
                                        showSuccessInfo(res.data.message);
                                }

                                let successIndex = [];
                                res.data.successIds.map((successId) => {
                                        successIndex.push(indexMap.get(successId));
                                        // 删除缓存对应的pk数据
                                        deleteCacheDataForList(props, AREA.listTableId, successId);
                                });
                                props.table.deleteTableRowsByIndex(AREA.listTableId, successIndex);
                                //更新按钮状态
                                buttonControl.call(this, props, props.table.getCheckedRows(AREA.listTableId));
                        }
                }
        });
}
